#include "fulfillmentdialog.h"
#include "ui_fulfillmentdialog.h"

#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include <QDate>
FulfillmentDialog::FulfillmentDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::FulfillmentDialog)
{
    ui->setupUi(this);
    setWindowTitle("采购履约管理");

    //初始化下拉框
    ui->cmbStatus->addItems({"待发货", "已发货", "已送达"});

}

void FulfillmentDialog::setOrderId(int poId)
{
    currentOrderId = poId;
    //加载订单基本信息
    QSqlQuery query;
    query.prepare("SELECT po.order_date, s.name, po.total_amount "
                  "FROM PurchaseOrder po "
                  "JOIN Supplier s ON po.supplier_id = s.supplier_id "
                  "WHERE po.po_id = :poId");
    query.bindValue(":poId", poId);

    if (query.exec() && query.next())
    {
        ui->lblOrderId->setText(QString("采购订单编号：#%1").arg(poId));
        ui->lblSupplier->setText(QString("供应商：%1").arg(query.value(1).toString()));
        orderDate = query.value(0).toDate();
        ui->lblOrderDate->setText(QString("下单日期：%1").arg(orderDate.toString("yyyy-MM-dd")));
        ui->lblTotalAmount->setText(QString("总金额：%1").arg(query.value(2).toDouble()));
    }

    //加载采购履约信息
    query.prepare("SELECT eta, actual_arrival, ship_status "
                  "FROM PurchaseFulfillment "
                  "WHERE po_id = :poId");
    query.bindValue(":poId", poId);

    if (query.exec() && query.next())
    {
        QDate etaDate = query.value(0).toDate();
        ui->lblETA->setText(QString("预计到货日期：%1").arg(etaDate.toString("yyyy-MM-dd")));
        if (!query.value(1).isNull())
        {
            ui->dateActualArrival->setDate(query.value(1).toDate());
        }
        ui->cmbStatus->setCurrentText(query.value(2).toString());
    }

    ui->dateActualArrival->setDate(orderDate);
}



FulfillmentDialog::~FulfillmentDialog()
{
    delete ui;
}

void FulfillmentDialog::on_buttonBox_accepted()
{
    QDate actualArrival = ui->dateActualArrival->date();
    QString status = ui->cmbStatus->currentText();

    //验证数据
    if (status.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请选择履约状态");
        return;
    }
    if (actualArrival.isValid() && actualArrival < orderDate)
    {
        QMessageBox::warning(this, "警告", "实际到货日期不能早于下单日期");
        return;
    }

    //更新数据库
    QSqlQuery query;
    query.prepare("UPDATE PurchaseFulfillment "
                  "SET actual_arrival = :actualArrival, ship_status = :status "
                  "WHERE po_id = :poId");
    query.bindValue(":actualArrival", actualArrival);
    query.bindValue(":status", status);
    query.bindValue(":poId", currentOrderId);

    if (query.exec()) {
        // 如果状态是"已送达"或"已发货"，更新订单状态为"已送达"或"已发货"
        if (status != "待发货") {
            QSqlQuery updateOrder;
            updateOrder.prepare("UPDATE PurchaseOrder SET order_status = :status WHERE po_id = :poId");
            updateOrder.bindValue(":status", status);
            updateOrder.bindValue(":poId", currentOrderId);
            updateOrder.exec();
        }
        QMessageBox::information(this, "成功", "履约信息已更新");
        accept();
    } else {
        QMessageBox::critical(this, "错误", "更新履约信息失败: " + query.lastError().text());
    }
}


